package com.gitee.qdbp.general.thirdpart.biz.wechat.model;

import java.util.Properties;
import com.gitee.qdbp.general.thirdpart.api.wechat.model.IWechatConfig;
import com.gitee.qdbp.tools.utils.Config;

/**
 * 微信配置项<br>
 * 公众平台: mp.weixin.qq.com<br>
 * 开放平台: open.weixin.qq.com<br>
 * 商户平台: pay.weixin.qq.com<br>
 * <pre>
    ###### https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
    ## 支付用的APPID, 有可能是公众平台或商户平台的APPID
    wechat.pay.app_id = 
    ## 应用密钥
    wechat.pay.app_secret = 
    ## 加密密钥[微信商户平台(pay.weixin.qq.com)-账户设置-API安全-密钥设置]
    wechat.pay.encrypt_key = 
    ## 微信分配的商户号
    wechat.pay.mch_id = 
    ###### 公共参数
    ## 服务器异步通知页面路径: 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    wechat.pay.merchant.notify_url = {config:site.manager.outer.url}actions/wechat-pay/notify/
    ## 网页授权域名: 微信公众平台(mp.weixin.qq.com) -- 设置 -- 公众号设置 -- 功能设置 -- 网页授权域名
    ## 当前域名与此域名不同时需要配置, 通过此域名下的接口中转来获取网页授权, 否则会报redirect_uri域名与后台配置不一致
    wechat.authorize.domain = www.xxx.com
    wechat.authorize.proxy_url = https://{config:wechat.authorize.domain}/actions/wechat-pay/authorize/proxy.do
 *  </pre>
 */
public class WechatConfig implements IWechatConfig {

    private Config config;

    public WechatConfig(Properties properties) {
        this.config = new Config(properties);
    }

    /** {@inheritDoc} **/
    @Override
    public boolean isPaymentTest() {
        return "true".equalsIgnoreCase(config.getString("payment.test", false));
    }

    /** {@inheritDoc} **/
    @Override
    public String getAuthorizeApiUrl() {
        return "https://open.weixin.qq.com/connect/oauth2/authorize";
    }
    
    /** {@inheritDoc} **/
    @Override
    public String getAuthorizeProxyUrl() {
        return config.getString("wechat.authorize.proxy_url", true);
    }

    /** {@inheritDoc} **/
    @Override
    public String getAccountTokenApiUrl() {
        return "https://api.weixin.qq.com/sns/oauth2/access_token";
    }

    public String getUnifiedOrderPayApiUrl() {
        return "https://api.mch.weixin.qq.com/pay/unifiedorder";
    }

    public String getUnifiedOrderQueryApiUrl() {
        return "https://api.mch.weixin.qq.com/pay/orderquery";
    }

    public String getUnifiedOrderCloseApiUrl() {
        return "https://api.mch.weixin.qq.com/pay/closeorder";
    }

    /** {@inheritDoc} **/
    @Override
    public String getPaymentUrlPrefix() {
        return config.getString("payment.url.prefix", false);
    }

    /** {@inheritDoc} **/
    @Override
    public String getMerchantNotifyUrl() {
        return config.getString("wechat.pay.merchant.notify_url");
    }
    
    /** {@inheritDoc} **/
    @Override
    public String getAuthorizeDomain() {
        return config.getString("wechat.authorize.domain", false);
    }

    /** 支付AppId **/
    public String getPayAppId() {
        return config.getStringUseDefKeys("wechat.pay.app_id", "wechat.app_id");
    }

    /** 支付AppSecret **/
    public String getPayAppSecret() {
        return config.getStringUseDefKeys("wechat.pay.app_secret", "wechat.app_secret");
    }

    /** 支付EncryptKey **/
    public String getPayEncryptKey() {
        return config.getStringUseDefKeys("wechat.pay.encrypt_key", "wechat.encrypt_key");
    }

    /** 商户MerchantId **/
    public String getPayMerchantId() {
        return config.getStringUseDefKeys("wechat.pay.mch_id", "wechat.mch_id");
    }

}
